package com.zyms.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取，properties
 * 
 * @author chenlujun
 * @version [版本号, 2014年11月8日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class PropertyUtils {

	private static final String PATH = "/conf/pay.properties";

	protected static Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
	   
	private static Properties props = null;

	private static long start = 0L;

	/**
	 * <一句话功能简述> <功能详细描述> [参数说明]
	 * 
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	private static void init() {
		long end = System.currentTimeMillis();
		// 为空时或加载超过1小时
		if (props == null || end - start >= 3600000) {
			start = end;

			InputStream in = null;
			props = new Properties();
			try {
				in = PropertyUtils.class.getResourceAsStream(PATH);
				props.load(in);
			} catch (IOException e) {
				logger.error("读取出错配置文件" + PATH + "出错", e);
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException ex) {
						logger.error("关闭IO异常", ex);
					}
				}
			}
		}
	}

	/**
	 * 获取属性文件值
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		init();
		return props.getProperty(key);
	}
}
